package com.dbappsecurity.jsbridge.webview;

import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Message;
import android.support.annotation.Keep;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.FrameLayout;
import com.tencent.smtt.export.external.interfaces.ConsoleMessage;
import com.tencent.smtt.export.external.interfaces.GeolocationPermissionsCallback;
import com.tencent.smtt.export.external.interfaces.IX5WebChromeClient;
import com.tencent.smtt.export.external.interfaces.JsPromptResult;
import com.tencent.smtt.export.external.interfaces.JsResult;
import com.tencent.smtt.sdk.ValueCallback;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebStorage;
import com.tencent.smtt.sdk.WebView;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public abstract class BWebChromeClient extends WebChromeClient {

  BWebView mBWebView;

  protected BWebChromeClient(BWebView BWebView) {
    mBWebView = BWebView;
  }

  @Override
  public void onReceivedTitle(WebView view, String title) {
    injectBaseJs();
  }


  @Override
  public abstract void openFileChooser(ValueCallback<Uri> valueCallback, String s, String s1);

  @Override
  public abstract boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> valueCallback,
      FileChooserParams fileChooserParams) ;

  @Override
  public boolean onJsAlert(WebView view, String url, final String message, final JsResult result) {
    if (mBWebView == null) return false;
    Dialog alertDialog = new AlertDialog.Builder(mBWebView.getContext()).
        setMessage(message).
        setCancelable(false).
        setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
            result.confirm();
          }
        })
        .create();
    alertDialog.show();
    return true;
  }


  @Override
  public boolean onJsConfirm(WebView view, String url, String message,
      final JsResult result) {
    if (mBWebView == null) return false;

      DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
          if (which == Dialog.BUTTON_POSITIVE) {
            result.confirm();
          } else {
            result.cancel();
          }
        }
      };
      new AlertDialog.Builder(mBWebView.getContext())
          .setMessage(message)
          .setCancelable(false)
          .setPositiveButton(android.R.string.ok, listener)
          .setNegativeButton(android.R.string.cancel, listener).show();
      return true;

  }


  private void injectBaseJs() {
    String string = null;
    AssetManager am = mBWebView.getContext().getResources().getAssets();
    InputStream inputStream = null;
    try {
      String[] files = am.list("js");
      for (String fileName : files) {
        inputStream = am.open("js/" + fileName);
        string = readTextFile(inputStream);
        mBWebView.evaluateJavascript(
            string
        );
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  private static String readTextFile(InputStream inputStream) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    byte buf[] = new byte[1024];
    int len;
    try {
      while ((len = inputStream.read(buf)) != -1) {
        outputStream.write(buf, 0, len);
      }
      outputStream.close();
      inputStream.close();
    } catch (IOException e) {
    }
    return outputStream.toString();
  }

}
